﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper.Currency;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Yande.Pool
{
    internal class YandePoolScanner
    {
        public static void AutoScanner(string PoolUrl)
        {
            int PoolId = GetPoolId(PoolUrl);
            Console.WriteLine("PoolId" + PoolId);

            StartScanner(PoolId);
        }

        /// <summary>
        /// 启动扫描，需要ID
        /// </summary>
        /// <param name="poolId"></param>
        private static void StartScanner(int PoolId)
        {
            if (PoolId == 0)
            {
                MessageHelper.Alert("错误的链接，解析ID失败！");
                return;
            }

            String Shtml = WebHelper.HttpPcGet("https://yande.re/pool/show.xml?id=" + PoolId);
            string Title = HtmlHelper.GetFirstAttributes(Shtml, "name", "//pool");
            List<string> ImgList = HtmlHelper.GetAttributesVlaueList(Shtml, "file_url", "//posts/post");
            foreach (string line in ImgList)
            {
                Console.WriteLine(line);
            }
            Console.WriteLine(Title);
            WebCurrency.SaveHelper.AutoSave(ImgList, "Yande", Title);
            Console.WriteLine("下载完成");
        }

        internal static void ScannerListPage(string PageString)
        {
            int PageCount = TextHelper.StringToInt(PageString);
            if (PageCount == 0)
            {
                MessageHelper.Alert("错误的数字，请输入大于0的链接！");
                return;
            }
            String Shtml = WebHelper.HttpPcGet("https://yande.re//pool.xml?page=" + PageCount);
            List<string> PoolIdList = HtmlHelper.GetAttributesVlaueList(Shtml, "id", "//pools/pool");
            int DownloadCount = 0;
            foreach (string line in PoolIdList)
            {
                AutoScanner(line);
                Console.WriteLine("扫描{0}，总共{1}个，已下载{2}个 ", line, PoolIdList, ++DownloadCount);
            }
        }

        /// <summary>
        /// 解析获取池ID
        /// </summary>
        /// <param name="PoolUrl"></param>
        /// <returns></returns>
        private static int GetPoolId(string PoolUrl)
        {
            int PoolId = TextHelper.StringToInt(PoolUrl);
            if (PoolId == 0)
            {
                PoolUrl = TextHelper.RegexText(PoolUrl, "yande.re/pool/show/[0-9]{1,}").Replace("yande.re/pool/show/", "");
                PoolId = TextHelper.StringToInt(PoolUrl);
            }
            return PoolId;
        }
    }
}